﻿using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace JianZhangTool.tool
{
    public static class Tools
    {
        /// <summary>
        /// 读取流
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static async Task ReadBAsync(this Stream stream, byte[] buffer, int offset, int count)
        {
            if (offset + count > buffer.Length)
            {
                throw new ArgumentException();
            }
            int read = 0;
            while (read < count)
            {
                var available = await stream.ReadAsync(buffer, offset, count - read);
                if (available == 0)
                {
                    throw new ObjectDisposedException(null);
                }
                read += available;
                offset += available;
            }
        }

        /// <summary>
        /// 字节流去除尾部0
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static byte[] bytesTrimEnd(byte[] bytes)
        {
            if (bytes[bytes.Length - 1] != 0x00)
            {
                return bytes;
            }
            List<byte> list = bytes.ToList();
            for (int i = bytes.Length - 1; i >= 0; i--)
            {
                if (bytes[i] == 0x00)
                {
                    list.RemoveAt(i);
                }
                else
                {
                    break;
                }
            }
            return list.ToArray();
        }

        /// <summary>
        /// 拷贝流
        /// </summary>
        /// <param name="input"></param>
        /// <param name="output"></param>
        public static void CopyStream(Stream input, Stream output)
        {
            byte[] buffer = new byte[2000];
            int len;
            while ((len = input.Read(buffer, 0, 2000)) > 0)
            {
                output.Write(buffer, 0, len);
            }
            output.Flush();
        }

        /// <summary>
        /// 弹出提示框
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="title"></param>
        /// <param name="text"></param>
        public static void alert_message(string title, string text)
        {
            Main.Instance.show_message(title, text);
        }

        /// <summary>
        /// 获取Excel单元格的字符串
        /// </summary>
        /// <param name="tCell"></param>
        /// <returns></returns>
        public static string get_cell_str(ICell tCell)
        {
            if (tCell == null)
            {
                return "";
            }
            string tempValue = "";
            switch (tCell.CellType)
            {
                case CellType.Blank:
                    break;
                case CellType.Boolean:
                    tempValue = tCell.BooleanCellValue.ToString();
                    break;
                case CellType.Error:
                    break;
                case CellType.Formula:
                    IFormulaEvaluator fe = WorkbookFactory.CreateFormulaEvaluator(tCell.Sheet.Workbook);
                    var cellValue = fe.Evaluate(tCell);
                    switch (cellValue.CellType)
                    {
                        case CellType.Blank:
                            break;
                        case CellType.Boolean:
                            tempValue = cellValue.BooleanValue.ToString();
                            break;
                        case CellType.Error:
                            break;
                        case CellType.Formula:
                            break;
                        case CellType.Numeric:
                            tempValue = cellValue.NumberValue.ToString();
                            break;
                        case CellType.String:
                            tempValue = cellValue.StringValue.ToString();
                            break;
                        case CellType.Unknown:
                            break;
                        default:
                            break;
                    }
                    break;
                case CellType.Numeric:
                    if (DateUtil.IsCellDateFormatted(tCell))
                    {
                        tempValue = tCell.DateCellValue.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        tempValue = tCell.NumericCellValue.ToString();
                    }
                    break;
                case CellType.String:
                    tempValue = tCell.StringCellValue.Trim();
                    break;
                case CellType.Unknown:
                    break;
                default:
                    break;
            }
            return tempValue;
        }
    }
}
